package com.linkage.educloud.ah.view.activity.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.adapter.VClassAdapter;
import com.linkage.educloud.ah.bean.AccountData;
import com.linkage.educloud.ah.bean.VClassBean;
import com.linkage.educloud.ah.config.AccountConfig;
import com.linkage.educloud.ah.mvp.contract.mine.IVClassContract;
import com.linkage.educloud.ah.mvp.presenter.mine.VClassPresenter;
import com.linkage.educloud.ah.view.BasicTitleActivity;
import com.linkage.educloud.ah.widget.NoScrollListView;

import java.text.SimpleDateFormat;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Mr'Tang on 2018/12/11.
 */

public class VClassActivity extends BasicTitleActivity implements IVClassContract.IView {

    @BindView(R.id.ac_vclass_tips)
    TextView ac_vclass_tips;
    @BindView(R.id.ac_vclass_isshow)
    LinearLayout ac_vclass_isshow;
    @BindView(R.id.ac_vclass_free)
    TextView ac_vclass_free;
    @BindView(R.id.ac_vclass_bar)
    ProgressBar ac_vclass_bar;
    @BindView(R.id.ac_vclass_bar_text)
    TextView ac_vclass_bar_text;
    @BindView(R.id.ac_vclass_used)
    TextView ac_vclass_used;
    @BindView(R.id.ac_vclass_listview)
    NoScrollListView ac_vclass_listview;

    IVClassContract.IPresenter iPresenter;
    AccountData accountData;

    public static void launch(Context context) {
        Intent intent = new Intent(context, VClassActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_vclass;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        accountData = AccountConfig.getLoginAccount();
        iPresenter = new VClassPresenter(this, this);
        showZloading();
        iPresenter.getVClassSurplus(accountData.getUserType(), accountData.getPhone());
    }

    public void initDateSuccess(int total, int used, List<VClassBean> list) {
        hideZloading();
        long now = System.currentTimeMillis();
        SimpleDateFormat format = new SimpleDateFormat("MM月dd日 HH:mm");
        ac_vclass_tips.setText("尊敬的用户，您的班级网免费通话当月使用情况如下（截至" + format.format(now) + "）");
        ac_vclass_free.setText("每月免费" + total + "分钟");
        if (accountData.getUserType() == 1) {
            ac_vclass_isshow.setVisibility(View.VISIBLE);
            double pro = 0;
            int progress = 0;
            if (total > 0) {
                pro = (double) (Math.round(used * 100) / (100.0 * total));
                progress = (int) Math.ceil(used * 100
                        * 100 / (100.0 * total));
            }
            ac_vclass_bar.setProgress(progress);
            ac_vclass_bar_text.setText(pro * 100 + "%");
            ac_vclass_used.setText("已使用" + used + "分钟，剩余"
                    + (total - used) + "分钟");
        } else {
            ac_vclass_isshow.setVisibility(View.GONE);
            if (list != null) {
                ac_vclass_listview.setAdapter(new VClassAdapter(VClassActivity.this, list));
            }
        }
    }

    public void initDateError() {
        long now = System.currentTimeMillis();
        SimpleDateFormat format = new SimpleDateFormat("MM月dd日 HH:mm");
        ac_vclass_tips.setText("尊敬的用户，您的班级网免费通话当月使用情况如下（截至" + format.format(now) + "）");
        if (accountData.getUserType() == 1) {
            ac_vclass_bar.setVisibility(View.VISIBLE);
            ac_vclass_bar_text.setVisibility(View.VISIBLE);
            ac_vclass_used.setVisibility(View.VISIBLE);
            ac_vclass_bar.setProgress(0);
            ac_vclass_bar_text.setText(0 + "%");
            ac_vclass_used.setText("已使用" + 0 + "分钟，剩余" + 500 + "分钟");
        } else {
            ac_vclass_bar.setVisibility(View.GONE);
            ac_vclass_bar_text.setVisibility(View.GONE);
            ac_vclass_used.setVisibility(View.GONE);
        }
    }
}
